﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _06_DoesMyNumberLookBigInThis
{
    public class Kata
    {
        public static bool Narcissistic(int value)
        {
            // Code me
            int numLength = value.ToString().Length;
            return SumOfPowers(value,numLength)==value;

            //更简洁的方法，求和函数中使用lamba表达式
            //var str = value.ToString();
            //return str.Sum(c => Math.Pow(Convert.ToInt16(c.ToString()), str.Length)) == value;
        }
        public static double SumOfPowers(int value,int numLength)
        {
            int x=0 ,y=0;
            double sum = 0;
            if (value < 10)
            {
                x = value;
                y = 0;
            }                
            else
            {
                x = value % 10;
                y = value / 10;
            }
            if (y > 0)
                sum = SumOfPowers(y, numLength) + Math.Pow(x, numLength);
            else
                sum += Math.Pow(x, numLength);
            return sum;
        }
    }
}
